package top.lywivan.mm.controller;

import top.lywivan.mm.common.GlobalConst;
import top.lywivan.mm.entity.PageResult;
import top.lywivan.mm.entity.QueryPageBean;
import top.lywivan.mm.entity.Result;
import top.lywivan.mm.pojo.Question;
import top.lywivan.mm.pojo.User;
import top.lywivan.mm.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/question")
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/findListByPage")
    public Result findListByPage(@RequestBody QueryPageBean queryPageBean){
        //System.out.println(queryPageBean);
        PageResult pageResult = questionService.findListByPage(queryPageBean);
        //System.out.println(pageResult);
        return new Result(true,"分页查询成功",pageResult);
    }

    @PostMapping("/addOrUpdate")
    public Result addOrUpdateQuestion(@RequestBody Question question, HttpSession session){
        //System.out.println(question);
        User user = (User) session.getAttribute(GlobalConst.SESSION_KEY_USER);
        if (user==null){
            user=new User();
            user.setId(1);
        }
        if (question.getUserId()==null){
            question.setUserId(user.getId());
        }
        if(question.getId()==0){
            questionService.addQuestion(question);
            return new Result(true,"添加成功");
        }else {
            questionService.updateQuestion(question);
            return new Result(true,"修改成功");
        }
    }

    //TODO: 修改题目出现了问题！！！！
    @GetMapping("/findById")
    public Result findQuestionById(Integer questionId){
        Question question =  questionService.findQuestionById(questionId);
        System.out.println("结果为："+question);
        return new Result(true,"根据id查询题目成功",question);
    }

    @PostMapping("/findClassicListByPage")
    public Result findClassicListByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = questionService.findClassicListByPage(queryPageBean);
        return new Result(true,"分页查询成功",pageResult);
    }

    /*@GetMapping("/findById")
    public Result findQuestionById(Integer questionId){
        Question question = questionService.findQuestionById(questionId);
        return new Result(true,"获取成功",question);
    }*/
}
